package com.lg.atp.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.lg.atp.common.Constants;
import com.lg.atp.entity.TblPara;
import com.lg.atp.entity.TblParaData;
import com.lg.atp.service.IParaService;

@RestController
@RequestMapping("para")
public class ParaController {

	private static final Log log = LogFactory.getLog(ParaController.class);
	@Autowired
	private IParaService iParaService;
	
	//选择变量
	@PostMapping("getOperSelData")
	public Map<String,Object> getOperSelData(String paraIds){
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("status", Constants.RETURN_SUCCESS);
		try {
			List<Long> ids = new ArrayList<Long>();
			if(StringUtils.isNotBlank(paraIds))
			for(String id:paraIds.split(",")) {
				ids.add(Long.valueOf(id));
			}
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("list", ids);
			List<TblPara> list = iParaService.getSelPara(map);
			result.put("data", list);
		}catch(Exception e) {
			e.printStackTrace();
			log.error("获取带变量值的变量失败："+e.getMessage());
			result.put("status", Constants.RETURN_FAIL);
			result.put("errorMsg", "获取已有的变量失败");
		}
		
		return result;
	}
	
	@PostMapping("getOperCrData")
	public Map<String,Object> getOperCrData(String paraIds){
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("status", Constants.RETURN_SUCCESS);
		try {
			List<Long> ids = new ArrayList<Long>();
			if(StringUtils.isNotBlank(paraIds))
			for(String id:paraIds.split(",")) {
				ids.add(Long.valueOf(id));
			}
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("list", ids);
			List<TblPara> list = iParaService.getCrPara(map);
			result.put("data", list);
		}catch(Exception e) {
			e.printStackTrace();
			log.error("获取存入变量失败："+e.getMessage());
			result.put("status", Constants.RETURN_FAIL);
			result.put("errorMsg", "获取已有的变量失败");
		}
		
		return result;
	} 
	
	@PostMapping("saveCrPara")
	public Map<String,Object> saveCrPara(TblPara para){
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("status", Constants.RETURN_SUCCESS);
		try {
			boolean hasExists = iParaService.hasExistsPara(para);
			if(hasExists) {
				result.put("status", Constants.RETURN_FAIL);
				result.put("errorMsg", "变量名已存在");
				return result;
			}
			iParaService.saveParaData(para);
			result.put("data", para);
		}catch(Exception e) {
			e.printStackTrace();
			log.error("保存存入失败:"+e.getMessage());
			result.put("status", Constants.RETURN_FAIL);
			result.put("errorMsg", "存入变量失败");
		}
		return result;
	}
	
	@PostMapping("changeParaData")
	public Map<String,Object> changeParaData(Long paraId,String paraData){
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("status", Constants.RETURN_SUCCESS);
		try {
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("paraId", paraId);
		    map.put("paraData", paraData);
			iParaService.changeParaData(map);
		}catch(Exception e) {
			e.printStackTrace();
			log.error("保存存入失败:"+e.getMessage());
			result.put("status", Constants.RETURN_FAIL);
			result.put("errorMsg", "存入变量失败");
		}
		return result;
	}
	
	
	@PostMapping("saveParaData")
	public Map<String,Object> saveParaData(String paraJson,String paraDataJson){
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("status", Constants.RETURN_SUCCESS);
		try {
			TblPara para = JSONObject.parseObject(paraJson,TblPara.class);
			boolean hasExists = iParaService.hasExistsPara(para);
			if(hasExists) {
				result.put("status", Constants.RETURN_FAIL);
				result.put("errorMsg", "参数名已存在");
				return result;
			}
			List<TblParaData> paraDataList = JSONObject.parseArray(paraDataJson, TblParaData.class);
			para.setParaDataList(paraDataList);
			iParaService.saveParaData(para);
			result.put("data", para);
		}catch(Exception e) {
			e.printStackTrace();
			log.error("保存变量值失败:"+e.getMessage());
			result.put("status", Constants.RETURN_FAIL);
			result.put("errorMsg", "选择变量失败");
		}
		return result;
	}
	
	@PostMapping("getParaByDataset")
	public Map<String,Object> getParaByDataset(Long datasetId,Integer paraType){
		
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("status", Constants.RETURN_SUCCESS);
		try {
			TblPara para = new TblPara();
			para.setDatasetId(datasetId);
			para.setParaType(paraType);
			List<TblPara> list = iParaService.selectParaByDatasetId(para);
			result.put("data", list);
		}catch(Exception e) {
			e.printStackTrace();
			log.error("获取数据集参数失败:"+e.getMessage());
			result.put("status", Constants.RETURN_FAIL);
			result.put("errorMsg", "获取参数失败");
		}
		return result;
	}
	
	
	@PostMapping("getParaDataByParaId")
	public Map<String,Object> getParaDataByParaId(Long paraId){
		
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("status", Constants.RETURN_SUCCESS);
		try {
			List<TblParaData> list = iParaService.selectParaDataByParaId(paraId);
			result.put("data", list);
		}catch(Exception e) {
			e.printStackTrace();
			log.error("获取参数值失败:"+e.getMessage());
			result.put("status", Constants.RETURN_FAIL);
			result.put("errorMsg", "获取参数值失败");
		}
		return result;
	}
}
